//
//  QBRootViewController.swift
//  QBDropDownMenuDemo
//
//  Created by 乐启榜 on 16/7/27.
//  Copyright © 2016年 乐启榜. All rights reserved.

//  注意：
//  Menu的类写在了Tableview的HeadView里

import UIKit

class QBRootViewController: UIViewController {
    
    var _mainTitles = Array<String>()
    var _subTitles = Array<Array<String>>()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        _mainTitles = ["BOOKS", "MOVIES", "HOME", "MUSIC"]
        _subTitles = [
            ["All", "Arts", "Photography", "Biographies", "Memoirs", "Business", "Calendars", "Education", "Comics", "Computers", "Transpotation", "History"],
            ["All", "Animation", "Drama", "Comedy"],
            ["All", "Furniture", "Bedding", "Bath", "Artwork"],
            ["All", "Blues", "Christian", "Classical", "Country", "Jazz", "Pop", "Folk", "Gospel"]]
        
        weak var weakSelf = self
        
        let tableView: UITableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)
        tableView.snp_makeConstraints { (make) in
            make.edges.equalTo((weakSelf?.view)!)
        }

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

// MARK: - QBDropDownMenuDataSource & QBDropDownMenuDelegate
extension QBRootViewController: QBDropDownMenuDataSource,QBDropDownMenuDelegate {
    
    // QBDropDownMenuDataSource
    func numberOfColumnsInMenu(menu: QBDropDownMenu) -> (Int) {
        return _mainTitles.count
    }
    func menu(menu menu: QBDropDownMenu, numberOfRowsInColumns columns: Int) -> (Int) {
        return _subTitles[columns].count
    }
    func menu(menu menu: QBDropDownMenu, titleForColumn column: Int) -> (String) {
        return _mainTitles[column]
    }
    func menu(menu menu: QBDropDownMenu, titleForRowAtIndexPath index: QBIndexPath) -> (String) {
        let arr: Array = _subTitles[index._column]
        return arr[index._row]
    }
    
    // QBDropDownMenuDelegate
    func menu(menu menu: QBDropDownMenu, didSelectedRowAtIndexPath index: QBIndexPath) {
        let array = self._subTitles[index._column]
        print(array[index._row])
    }
}

// MARK: - UITableViewDelegate & UITableViewDataSource
extension QBRootViewController: UITableViewDelegate,UITableViewDataSource {
    
    // UITableViewDataSource
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let identifier = "cell"
        var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(identifier)
        if cell == nil {
            cell = UITableViewCell(style: .Default, reuseIdentifier: identifier)
        }
        
        cell?.textLabel?.text = "123"
        
        return cell!
    }
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

            let menuView = QBDropDownMenu()
            menuView.frame = CGRectZero
            menuView.delegate = self
            menuView.dataSource = self
        
            return menuView
    }
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

        return 50
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 50
    }
    
    // UITableViewDelegate
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//       print("Did Selected \(indexPath.row)")
    }
}

